<?php get_instance()->view('common/header');?>
<script type="text/javascript" src="http://api.map.baidu.com/getscript?v=2.0&ak=DpM2LLeXhxIaS6jMmIGcCYo93rEvtRAV"></script>
<style>
    #uploader-demo .uploader-list img{width: 120px;height: 100px}
    #uploader-demo .thumbnail {width: 120px;height: 100px}
    #filePicker .webuploader-pick img{width: 110px;height: 90px}
    .tangram-suggestion-main{z-index: 999999}
</style>
<div class="page-container home">
    <?php get_instance()->view('common/sidebar');?>
    <div class="page-content-wrapper">
        <div class="page-content">
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <h3 class="page-title"> <?php echo $title;?></h3>
                    <ul class="page-breadcrumb breadcrumb">
                        <li class="btn-group"> <a class="btn default ml_5 return" href="<?php echo getUrl('find/home');?>"><i class="fa fa-chevron-left"></i> 返回</a> </li>
                        <li><i class="fa fa-home"></i><a href="<?php echo getUrl('find/home');?>">信息管理 </a><i class="fa fa-angle-right"></i></li>
                        <li><a href="javascript:void(0);"><?php echo $add ? '添加' : '编辑';?></a></li>
                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN VALIDATION STATES-->
                    <div class="portlet box">
                        <div class="portlet-body form">
                            <!-- BEGIN FORM-->
                            <form action="<?php echo getUrl('find/home/save')?>" class="form-horizontal" method="post" id="main-form" novalidate="novalidate">
                                <div class="form-body">
                                    <div class="form-group">
                                        <label class="control-label col-md-2">信息分类<span class="required">*</span></label>
                                        <div class="col-md-7" >
                                            <select class="form-control sel select2me" name="category_id">
                                                <option attr ='0' value="">请选择</option>
                                                <?php foreach ($category as $k => $v): ?>
                                                    <option attr = "<?php echo isset($v['attr_cate_id'])?$v['attr_cate_id']:0; ?>" value="<?php echo $v['id']?>" <?php if (!$add && $info['category_id'] == $v['id']):?> selected <?php endif;?> ><?php echo $v['name']?></option>
                                                <?php endforeach;?>
                                            </select>
                                        </div>
                                        <script type="text/javascript">
                                            $(function () {
                                               var opt = '';
                                               <?php foreach ($province as $p):  ?>
                                                opt += '<option value="<?php echo $p['id']?>"><?php echo $p['name']?></option>';
                                                <?php endforeach;?>

                                                $(".sel").change(function () {
                                                    var  id = $(this).find("option:selected").attr("attr")

                                                    var url = "<?php echo getUrl('find/home/getAttributeGetByCategoryId')?>";
                                                    l_blockUI();
                                                    $.post(url,{category_id:id},function(data){
                                                        var data = data.data;
                                                        // 1文本框输入(纯数字) 2文本框输入(文本) 3选择输入 4多行文本输入  5城市类
                                                        var html = '';
                                                        $.each(data,function (key, obj) {
                                                            var t = obj.attr_input_type;
                                                            var name = 'attr_value['+obj.id+']';
                                                            if (t != 5) {
                                                                html += '<div class="form-group">' +
                                                                    '<label class="control-label col-md-2">' + obj.attr_name + '<span class="required">*</span></label>' +
                                                                    '<div class="col-md-7">';
                                                                if (t == 1) {
                                                                    html += '<input class="form-control number" name="' + name + '" type="text">';
                                                                } else if (t == 2) {
                                                                    html += '<input class="form-control" name="' + name + '" type="text">';
                                                                } else if (t == 3) {
                                                                    html += '<select class="form-control sel select2me" name="' + name + '">' +
                                                                        '<option value="">请选择</option>';
                                                                        $.each(obj.attr_values, function (k, v) {
                                                                            html += '<option value="' + v + '">' + v + '</option>';
                                                                        });
                                                                    html += '</select>';
                                                                } else if (t == 4) {
                                                                    html += '<textarea  class="form-control required" name="' + name + '" ></textarea>';
                                                                }
                                                                html += '</div></div>';
                                                            }else{
                                                                html += '<div class="form-group" id="region" data-url="<?php echo getUrl('common/ajaxChangeCheck');?>">'+
                                                                        '<label class="control-label col-md-2">'+obj.attr_name+'<span class="required">*</span></label>'+
                                                                        '<div class="col-xs-2">'+
                                                                            '<select  id="province" name="attr_value['+obj.id+'][province]" class="form-control">'+
                                                                                '<option  value="">选择省份</option>'+opt+
                                                                            '</select>'+
                                                                        '</div>'+
                                                                        '<div class="col-xs-2">'+
                                                                            '<select  id="city" name="attr_value['+obj.id+'][city]" class="form-control">'+
                                                                                '<option value="">选择城市</option>'+
                                                                            '</select>'+
                                                                        '</div>'+
                                                                        '<div class="col-xs-3">'+
                                                                            '<select id="county" name="attr_value['+obj.id+'][county]" class="form-control">'+
                                                                                '<option value="">选择区县</option>'+
                                                                            '</select>'+
                                                                        '</div>'+
                                                                    '</div>';
                                                            }
                                                        });
                                                        $(".attr-box").html(html);
                                                        $('select').select2();
                                                        App.unblockUI();
                                                    }, 'json');
                                                });
                                            });
                                        </script>
                                    </div>
                                    <!--分类属性-->
                                    <div class="form-group attribute"></div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">标题</label>
                                        <div class="col-md-7">
                                            <input class="form-control " name="title" type="text" value="<?php echo ! $add ? $info['title'] : '';?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">内容</label>
                                        <div class="col-md-7">
                                            <textarea  class="form-control required" name="content" ><?php echo ! $add ? $info['content'] : ''?></textarea>
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">图片<span class="required">*</span></label>
                                        <div class="col-md-7">
                                            <div id="uploader-demo">
                                                <!--用来存放item-->
                                                <div class="uploader-list">
                                                    <?php if (! $add && $info['pic']):?>
                                                        <?php  foreach ($info['pic'] as $v):?>
                                                            <div id="" class="file-item thumbnail pic-list">
                                                                <img src="<?php echo $v; ?>">
                                                                <a title="删除" class="delete-btn hide" href="javascript:void(0);" style="color:#b94a48;position:absolute;top:-10px;right:-5px;background-color:#ffff;height:17px;width:15px;font-size:12px;padding:2px;">
                                                                    <i class="fa fa-times"></i>
                                                                </a>
                                                                <input name="pic[]" type="hidden" class="file-name" value="<?php echo $v; ?>">
                                                            </div>
                                                        <?php endforeach; endif;?>
                                                    <div id="filePicker" class="file-item thumbnail webuploader-container">
                                                        <div class="webuploader-pick">
                                                            <div style="position:absolute;top:0;left:0;width:100%;height:100%;padding-top:70px;color:grey;">选择图片</div>
                                                            <img title="选择上传图片" src="<?php echo getDefaultImg(); ?>">
                                                        </div>
                                                        <div id="rt_rt_1c2t7atu83351ra0bvm7gcgnu1" style="position: absolute; top: 4px; left: 4px; width: 100px; height: 100px; overflow: hidden; bottom: auto; right: auto;">
                                                            <input type="file" name="file" class="webuploader-element-invisible" multiple="multiple" accept="image/*">
                                                            <label style="opacity: 0; width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);">
                                                            </label>
                                                        </div>
                                                    </div>
                                                </div>
                                                <script type="text/javascript">
                                                    $(function(){
                                                        uploadImg({
                                                            box:'#uploader-demo',
                                                            uploadBtn:"#filePicker",
                                                            fileName:'pic[]',
                                                            width:110,  //显示缩略图宽带
                                                            uploadUrl:'<?php echo getUrl('find/home/uploadImgs');?>',
                                                            height:100,	//显示缩略图高度
                                                            number:3
                                                        });

                                                    });

                                                </script>
                                            </div>
                                        </div>
                                    </div>


                                    <div class="attr-box">
                                        <?php if (!$add && !empty($attribute)): foreach ($attribute as $key => $obj): $t = $obj['attr_input_type'];
                                            $name = 'attr_value['.$obj['id'].']'; $value = $obj['attr_value'];?>
                                            <?php if ($t != 5):?>
                                                <div class="form-group">
                                                    <label class="control-label col-md-2"><?php echo $obj['attr_name']?><span class="required">*</span></label>
                                                    <div class="col-md-7">
                                                    <?php if ($t == 1) { ?>
                                                        <input class="form-control number" name="<?php echo $name?>" type="text" value="<?php echo $value?>">
                                                    <?php } else if ($t == 2) {?>
                                                        <input class="form-control" name="<?php echo $name?>" type="text" value="<?php echo $value?>">
                                                    <?php }else if ($t == 3) {?>
                                                        <select class="form-control sel select2me" name="<?php echo $name?>">
                                                            <option value="">请选择</option>
                                                            <?php foreach ($obj['attr_values'] as $v):?>
                                                                <option <?php if($value == $v):?> selected <?php endif;?> value="<?php echo $value?>"><?php echo $v?></option>
                                                            <?php endforeach;?>
                                                        </select>
                                                    <?php }else if ($t == 4) {?>
                                                        <textarea  class="form-control required" name="<?php echo $name?>" ><?php echo $value?></textarea>
                                                    <?php }?>
                                                    </div>
                                                </div>
                                            <?php else:?>
                                                <div class="form-group" id="region" data-url="<?php echo getUrl('common/ajaxChangeCheck');?>">
                                                    <label class="control-label col-md-2"><?php echo $obj['attr_name']?><span class="required">*</span></label>
                                                    <div class="col-xs-2">
                                                        <select  id="province" name="attr_value[<?php echo $obj['id']?>][province]" class="form-control">
                                                            <option  value="">选择省份</option>
                                                            <?php foreach ($province as $p):  ?>
                                                            <option <?php if($value['province'] == $p['id']):?> selected <?php endif;?> value="<?php echo $p['id']?>"><?php echo $p['name']?></option>
                                                            <?php endforeach;?>
                                                        </select>
                                                    </div>
                                                    <div class="col-xs-2">
                                                        <select  id="city" name="attr_value[<?php echo $obj['id']?>][city]" class="form-control">
                                                            <option value="">选择城市</option>
                                                            <?php foreach ($value['city_list'] as $c):  ?>
                                                                <option <?php if($value['city'] == $c['id']):?> selected <?php endif;?> value="<?php echo $c['id']?>"><?php echo $c['name']?></option>
                                                            <?php endforeach;?>
                                                        </select>
                                                    </div>
                                                    <div class="col-xs-3">
                                                        <select id="county" name="attr_value[<?php echo $obj['id']?>][county]" class="form-control">
                                                            <option value="">选择区县</option>
                                                            <?php foreach ($value['county_list'] as $cy):  ?>
                                                                <option <?php if($value['county'] == $cy['id']):?> selected <?php endif;?> value="<?php echo $cy['id']?>"><?php echo $cy['name']?></option>
                                                            <?php endforeach;?>
                                                        </select>
                                                    </div>
                                                </div>
                                        <?php endif; endforeach; endif;?>
                                    </div>
                                    <!--区域-->
                                    <div class="form-group" id="primary_region">
                                        <label class="control-label col-md-2">区域<span class="required">*</span></label>
                                        <div class="col-xs-2">
                                            <select name="province" class="form-control">
                                                <option  value="">选择省份</option>
                                                <?php foreach ($province as $p):  ?>
                                                    <option <?php if(!$add && $info['province'] == $p['id']):?> selected <?php endif;?> value="<?php echo $p['id']?>"><?php echo $p['name']?></option>
                                                <?php endforeach;?>
                                            </select>
                                        </div>
                                        <div class="col-xs-2">
                                            <select name="city" class="form-control">
                                                <option value="">选择城市</option>
                                                <?php if (!$add): foreach ($city as $ciVal):  ?>
                                                    <option <?php if($info['city'] == $ciVal['id']):?> selected <?php endif;?> value="<?php echo $ciVal['id']?>"><?php echo $ciVal['name']?></option>
                                                <?php endforeach; endif;?>
                                            </select>
                                        </div>
                                        <div class="col-xs-3">
                                            <select name="county" class="form-control">
                                                <option value="">选择区县</option>
                                                <?php if (!$add):  foreach ($county as $coVal):  ?>
                                                    <option <?php if($info['county'] == $coVal['id']):?> selected <?php endif;?> value="<?php echo $coVal['id']?>"><?php echo $coVal['name']?></option>
                                                <?php endforeach;  endif;?>
                                            </select>
                                        </div>
                                        <script type="text/javascript">
                                            $(function () {
                                                var region,url;
                                                region = $("#primary_region");
                                                url = "<?php echo getUrl('common/ajaxChangeCheck');?>";

                                                //选择省份
                                                region.find('[name="province"]').live('change', function(){
                                                    var province=$(this).val();
                                                    if(province != ''){
                                                        $.post(url,{parent_id:province},function(re){
                                                            region.find("[name='city']").find("option").remove();
                                                            var city = "<option value=''>选择城市</option>";
                                                            $.each( re, function(i,item){
                                                                city += "<option value='" + item['id'] + "'>" + item['name'] + "</option>";
                                                            });
                                                            region.find("[name='city']").html(city);
                                                            region.find("[name='city']").select2();
                                                        },'json');
                                                        region.find("[name='county']").html("<option>选择区县</option>");
                                                        region.find("[name='county']").select2();
                                                    }
                                                });

                                                //选择城市
                                                region.find('[name="city"]').live('change', function(){
                                                    var city = $(this).val();
                                                    if(city!=''){
                                                        $.post(url,{parent_id:city},function(re){
                                                            region.find("[name='county']").find("option").remove();
                                                            var county = "<option>选择区县</option>";
                                                            $.each( re, function(i,item){
                                                                county += "<option value='" + item['id'] + "'>" + item['name'] + "</option>";
                                                            });
                                                            region.find("[name='county']").html(county);
                                                            region.find("[name='county']").select2();
                                                        },'json')
                                                    }
                                                });
                                            });
                                        </script>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">详情地址</label>
                                        <div class="col-md-7">
                                            <input class="form-control address" name="address" type="text" value="<?php echo ! $add ? $info['address'] : '';?>" readonly>
                                            <input class="form-control" name="latitude" type="hidden" value="<?php echo ! $add ? $info['latitude'] : '';?>">
                                            <input class="form-control" name="longitude" type="hidden" value="<?php echo ! $add ? $info['longitude'] : '';?>">
                                        </div>
                                    </div>
                                    <div class="form-group">
                                        <label class="control-label col-md-2">状态<span class="required">*</span></label>
                                        <div class="col-md-7" style="margin-left: 18px;">
                                            <div class="radio-list">
                                                <label class="radio-inline"><input name="status" value="2" type="radio" checked> 启用 </label>
                                                <label class="radio-inline" style="padding-left: 20px"><input name="status"  value="1" type="radio"> 禁用 </label>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="form-actionsc fluid">
                                        <div class="col-md-offset-2 col-md-9" style="padding-left: 0;">
                                            <input name="id" value="<?php echo ! $add ? $info['id'] : '';?>" type="hidden">
                                            <button class="btn green"><i class="fa fa-save"></i> 保存</button>
                                            <a class="btn default seach-btn" href="<?php echo getUrl('find/home');?>"><i class="fa fa-chevron-left"></i> 返回</a>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                    <!-- END VALIDATION STATES-->
                </div>
                <!-- END PAGE CONTENT-->
            </div>
        </div>
    </div>
</div>

<!--地图地址-->
<div id="save-modal" class="modal fade modal_small" role="dialog" aria-labelledby="myModalLabel1" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">地址</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">
                    <label class="control-label col-md-2">地址<span class="required">*</span></label>
                    <div class="col-md-9">
                        <input type="text" id="suggestId" size="20" class="form-control required" placeholder="请输入地址"/>
                    </div>
                    <input type="hidden" id="addr"/>
                    <input type="hidden" id="lat"/>
                    <input type="hidden" id="lng"/>
                </div>
            </div>
            <div style="position: relative;">
                <img style="position: absolute; left: 50%;top: 50%;z-index: 999;" src="<?php echo $this->staticUrl;?>img/dw.png" alt="">
                <div id="l-map" style="height: 500px;"></div>
            </div>
            <div id="searchResultPanel" style="z-index:9999999;border:1px solid #C0C0C0;width:150px;height:auto; display:none;"></div>
            <script type="text/javascript">
                // 百度地图API功能
                function G(id) {
                    return document.getElementById(id);
                }

                var map = new BMap.Map("l-map");
                map.centerAndZoom("重庆",12);                   // 初始化地图,设置城市和地图级别。

                var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                    {"input" : "suggestId"
                        ,"location" : map
                    });
                map.enableScrollWheelZoom(true); // 缩放地图

                // 中心位置移动
                map.addEventListener("dragend", function(){
                    var center = map.getCenter();
                    var geoc = new BMap.Geocoder();
                    geoc.getLocation(center, function(rs){
                        if (rs.surroundingPois.length){
                            var title = rs.surroundingPois[0];
                            $("#addr").val(title.title);
                            $("#suggestId").val(title.title);
                            $("#lat").val(title.point.lat);
                            $("#lng").val(title.point.lng);
                        }else{
                            $("#addr").val(rs.address);
                            $("#suggestId").val(rs.address);
                            $("#lat").val(center.lat);
                            $("#lng").val(center.lng);
                        }
                    });
                });

                ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
                    var str = "";
                    var _value = e.fromitem.value;
                    var value = "";
                    if (e.fromitem.index > -1) {
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }

                    str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                    value = "";
                    if (e.toitem.index > -1) {
                        _value = e.toitem.value;
                        value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    }
                    $("#addr").val(value);
                    str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                    G("searchResultPanel").innerHTML = str;
                });

                var myValue;
                ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
                    var _value = e.item.value;
                    myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
                    G("searchResultPanel").innerHTML ="onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                    setPlace();
                });

                function setPlace(){
                    map.clearOverlays();    //清除地图上所有覆盖物
                    function myFun(){
                        var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                        $("#lat").val(pp.lat);
                        $("#lng").val(pp.lng);
                        map.centerAndZoom(pp, 18);
//                        map.addOverlay(new BMap.Marker(pp));    //添加标注
                    }
                    var local = new BMap.LocalSearch(map, { //智能搜索
                        onSearchComplete: myFun
                    });
                    local.search(myValue);
                }
            </script>

            <div class="modal-footer">
                <input type="hidden" name="id" value=""/>
                <button class="btn default" data-dismiss="modal" aria-hidden="true"><i class="fa fa-times"></i> 关闭</button>
                <button type="submit" class="btn green field-submit-btn"><i class="fa fa-save"></i> 保存</button>
                <script type="text/javascript">
                    $(function(){
                        var model = $('#save-modal');

                        $('.address').live('click',function(){
                            model.find('.has-error').removeClass('has-error').find('.validate_message').remove();
                            model.find('.show_message_error').remove();
                            model.modal();
                        });

                        $(".field-submit-btn").click(function () {
                            var addr = $("#addr").val();
                            var lat = $("#lat").val();
                            var lng = $("#lng").val();
                            if(!addr.length || !lat.length || !lng.length){
                                show_modal_message({status:0, message:'请输入正确的地址'},model.find('.modal-body'));
                                return false;
                            }
                            $("[name='address']").val(addr);
                            $("[name='latitude']").val(lat);
                            $("[name='longitude']").val(lng);
                            $(this).prev().click();
                        });
                    });
                </script>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">
    $(function(){
        $("select").select2();

        $.e_validate.init('#main-form', {});

        $('[dp]').not("[noinit]").datepicker({
            changeMonth: true,
            autoclose: true,
            format: "yyyy-mm-dd",
            language: 'zh-CN'
        });

        $("[name='ads_position']").change(function () {
            var vl = $("[name='ads_position']").val();
            var merchant = $("[name='merchant']").val();
            if(vl == 2 || vl == 3 || vl == 4 || vl == 5 || merchant > 0){
                $("[name='url']").parents('.form-group').addClass('hide');
            }else{
                $("[name='url']").parents('.form-group').removeClass('hide');
            }
        })
    });

    $(function(){
        setTimeout("$('.form-actions').width($('.form-body').innerWidth()).removeClass('hide');", '500');
        window.onresize = function(){
            $('.form-actions').width($('.form-body').innerWidth());
        }
    });

</script>
</body>
</html>